import { defineStore } from 'pinia'
import { RouteRecordRaw, _RouteRecordBase } from 'vue-router'
import router from '../router'
import useStore from '.'

// 过滤
const routerFilter = (data: _RouteRecordBase) => {
    const meta: any = data.meta
    return Object.assign(meta, {path: data.path, name: data.name})
}

// 公用方法
export const mainStore = defineStore('main', {
    actions: {
        ready() {
            const { options, currentRoute } = router
            const home: RouteRecordRaw[] | undefined = options.routes[0].children
            const navRouter: RouteRecordRaw[] | undefined = home?.filter(item => item.meta && item.meta.isNav)
            const navMap = navRouter?.map(item => routerFilter(item))
            const { nav } = useStore()
            nav.setNavList(navMap)
        }
    }
})